#import ipaddr
#import late_command
#set $script = ""
##
##
#set ikeys = $interfaces.keys()
##################################
## configuring udev rules
##################################
#set $script += "### configuring udev rules ###\n"
#set $rules_file = "/etc/udev/rules.d/70-persistent-net.rules"
#set $script += "echo > %s\n" % $rules_file
#for $iname in $ikeys
    #set $idata = $interfaces[$iname]
    #set $imac = $idata.get("mac_address", "")
    #if $imac == "":
        #continue
    #end if
    #set $script += "MATCHADDR=%s INTERFACE=%s /lib/udev/write_net_rules\n" % ($imac.lower(), $iname)
#end for
#set $script += "\n"
#######################################
## configuring /etc/network/interfaces
#######################################
#set $script += "### configuring interfaces ###\n"
#set $interfaces_file = "/etc/network/interfaces"
#set $interfaces_dir = "/etc/network/interfaces.d"
#set $script += "echo source %s/*  > %s\n" % ($interfaces_dir,$interfaces_file)
#set $script += "mkdir %s\n" % $interfaces_dir
#set $script += "echo auto lo >> %s/ifcfg-lo \n" % $interfaces_dir
#set $script += "echo iface lo inet loopback >> %s/ifcfg-lo\n" % $interfaces_dir
#set $script += "echo >> %s/ifcfg-lo\n" % $interfaces_dir
#for $iname in $ikeys
    #set $idata = $interfaces[$iname]
    #set $interface_file = "%s/ifcfg-%s" % ($interfaces_dir,$iname)
    #set $onboot = $getVar("interface_extra_%s_onboot" % ($iname),"no")
    #if  $onboot == "yes"
        #set $script += "echo auto %s >> %s\n" % ($iname, $interface_file)
    #end if
    #if $idata.get("static", "0") in ("1", True, "true")
        #set $iip = $idata.get("ip_address", "")
        #if $iip != ""
            #set $script += "echo iface %s inet static >> %s\n" % ($iname, $interface_file)
            #set $script += "echo address %s >> %s\n" % ($iip, $interface_file)
            #set $inetmask = $idata.get("netmask", "")
            #if $inetmask != ""
                #set $script += "echo netmask %s >> %s\n" % ($inetmask, $interface_file)
            #end if
        #else
            #set $script += "echo iface %s inet manual >> %s\n" % ($iname, $interface_file)
            #set $script += "echo up ip l set %s up >> %s\n" % ($iname, $interface_file)
            #set $script += "echo down ip l set %s down >> %s\n" % ($iname, $interface_file)
        #end if
        #set $nss = ""
        #for $ns in $getVar("name_servers", [])
             #set $nss += " %s" % $ns
        #end for
        #set $gateway = $getVar("gateway", "")
        #if $gateway != ""
            #if $iip !=""
              #if $inetmask != ""
                #set $network_address = ipaddr.IPv4Network("%s/%s" % ($iip, $inetmask))
              #else
                #set $network_address = ipaddr.IPv4Network("%s" % $iip)
              #end if
              #set $gateway_address = ipaddr.IPv4Address($gateway)
              #if $gateway_address in $network_address
                #set $script += "echo gateway %s >> %s\n" % ($gateway, $interface_file)
              #end if
            #end if
        #end if
    #else
        #set $script += "echo iface %s inet dhcp >> %s\n" % ($iname, $interface_file)
    #end if
    #set $script += "echo >> %s\n" % $interface_file
#end for
#set $script += "\n"
#######################################
## configuring /etc/hosts
#######################################
#set $script += "### configuring /etc/hosts ###\n"
#set $hosts_file = "/etc/hosts"
#set $script += "echo > %s\n" % $hosts_file
#set $script += "echo 127.0.0.1 localhost >> %s\n" % $hosts_file
#for $iname in $ikeys
    #set $idata = $interfaces[$iname]
    #if $idata.get("static", "0") in ("1", True, "true")
        #set $iip = $idata.get("ip_address", "")
        #if $iip != ""
            #set $idnsname = $idata.get("dns_name", "")
            #if $idnsname != ""
                #set $script += "echo %s %s >> %s\n" % ($iip, $idnsname, $hosts_file)
            #end if
        #end if
    #end if
#end for
#######################################
## configuring domain name
#######################################
#set $script += "### configuring /etc/hosts ###\n"
#set $resolvconf = "/etc/resolv.conf"
#set $resolvhead = "/etc/resolvconf/resolv.conf.d/head"
#set $num_ns_search = $len($name_servers_search)
#if $num_ns_search > 0
  #set $domdom = $name_servers_search[0]
  #set $script += "echo domain %s >> %s\n" % ($domdom,$resolvconf)
  #set $script += "echo domain %s >> %s\n" % ($domdom,$resolvhead)
#end if
#if $name_servers
  #for $nameserver in $name_servers
      #set $script += "echo nameserver %s >> %s\n" % ($nameserver, $resolvconf)
      #set $script += "echo nameserver %s >> %s\n" % ($nameserver, $resolvhead)
  #end for
#end if
in-target $late_command.late_command($script, source_method="content") \
